﻿using System.Collections.Generic;
using UnityEngine;

namespace GameDesigner
{
    public class AnimationStateMachineView : StateMachineView
    {
        public new Animation animation;

        public override void Init(params object[] args)
        {
            controller.Init(this, (layer) => new AnimationStateMachine(animation, layer), args);
        }

#if UNITY_EDITOR
        public override void GetClipNames(Transform root, List<string> clipNames)
        {
            if (animation == null)
                animation = root.GetComponentInChildren<Animation>();
            if (animation != null)
            {
                var clips = UnityEditor.AnimationUtility.GetAnimationClips(animation.gameObject);
                clipNames.Clear();
                foreach (var clip in clips)
                    clipNames.Add(clip.name);
            }
            if (animClipReference is IAnimationClipReference animationClipReference)
            {
                clipNames.Clear();
                clipNames.AddRange(animationClipReference.ClipNames);
            }
        }

        public override float GetAnimClipLength(StateAction action)
        {
            var clip = animation.GetClip(action.clipName);
            if (clip == null)
                return 0f;
            return clip.length;
        }
#endif
    }
}